package com.hncc.sms.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.hncc.sms.domain.SysUserRoleManager;
import com.hncc.sms.mapper.SysUserRoleManagerMapper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hncc.sms.mapper.SysUserManagerMapper;
import com.hncc.sms.domain.SysUserManager;
import com.hncc.sms.service.ISysUserManagerService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2020-08-10
 */
@Service
public class SysUserManagerServiceImpl implements ISysUserManagerService
{
    @Resource
    private SysUserManagerMapper sysUserManagerMapper;

    /**
     * 用户角色类
     */
    @Resource
    private SysUserRoleManagerMapper sysUserRoleManagerMapper;

    /**
     * 查询用户信息
     *
     * @param userId 用户信息ID
     * @return 用户信息
     */
    @Override
    public SysUserManager selectSysUserManagerById(Long userId)
    {
        return sysUserManagerMapper.selectSysUserManagerById(userId);
    }

    /**
     * 查询用户信息列表
     *
     * @param sysUserManager 用户信息
     * @return 用户信息
     */
    @Override
    public List<SysUserManager> selectSysUserManagerList(SysUserManager sysUserManager)
    {
        return sysUserManagerMapper.selectSysUserManagerList(sysUserManager);
    }

    /**
     * 新增用户信息和用户角色信息
     *
     * @param sysUserManager 用户信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysUserManager(SysUserManager sysUserManager){
        sysUserManager.setCreateTime(DateUtils.getNowDate());
        //新增用户信息
        int rows = sysUserManagerMapper.insertSysUserManager(sysUserManager);
        //新增用户角色关联
        insertUserRole(sysUserManager);
        return rows;
    }

    /**
     * 修改用户信息
     *
     * @param sysUserManager 用户信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysUserManager(SysUserManager sysUserManager)
    {
        sysUserManager.setUpdateTime(DateUtils.getNowDate());
        //删除用户角色关联
        sysUserRoleManagerMapper.deleteSysUserRoleManagerById(sysUserManager.getUserId());
        //新增用户角色关联
        insertUserRole(sysUserManager);
        //修改用户信息
        return sysUserManagerMapper.updateSysUserManager(sysUserManager);
    }

    /**
     * 批量删除用户信息
     *
     * @param userIds 需要删除的用户信息ID
     * @return 结果
     */
    @Override
    public int deleteSysUserManagerByIds(Long[] userIds)
    {
        return sysUserManagerMapper.deleteSysUserManagerByIds(userIds);
    }

    /**
     * 删除用户信息信息
     *
     * @param userId 用户信息ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSysUserManagerById(Long userId)
    {
        int num = 0;
        if(sysUserRoleManagerMapper.deleteSysUserRoleManagerById(userId) > 0 &&
                sysUserManagerMapper.deleteSysUserManagerById(userId) > 0){
            num = 1;
        }
        return num;
    }

    /**
     * 校验用户名称是否唯一
     * @param userName
     * @return
     */
    @Override
    public String checkUserNameUnique(String userName) {
        System.out.println("用户--->"+sysUserManagerMapper.checkUserNameUnique(userName));
        if(sysUserManagerMapper.checkUserNameUnique(userName)!=null){
            //"1"
            return UserConstants.NOT_UNIQUE;
        }
        //"0"
        return UserConstants.UNIQUE;
    }

    /**
     * 查询所有参与排班的医生
     * @param deptId
     * @return
     */
    @Override
    public List<SysUserManager> selectSysUserManagerByDeptId(Long deptId) {
        return sysUserManagerMapper.selectSysUserManagerByDeptId(deptId);
    }

    /**
     * 新增用户角色信息
     *
     * @param user 用户对象
     */
    public void insertUserRole(SysUserManager user)
    {
        Long[] roles = user.getRoleIds();
        if (StringUtils.isNotNull(roles))
        {
            // 新增用户与角色管理
            List<SysUserRoleManager> list = new ArrayList<SysUserRoleManager>();
            for (Long roleId : roles)
            {
                SysUserRoleManager ur = new SysUserRoleManager();
                ur.setUserId(user.getUserId());
                ur.setRoleId(roleId);
                list.add(ur);
            }
            if (list.size() > 0)
            {
                sysUserRoleManagerMapper.insertSysUserRoleManager(list);
            }
        }
    }
}
